﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolLibrary.Entity;
using SchoolLibrary.Desktop.Guide;
using SchoolLibrary.DataBase.DAO;
using SchoolLibrary.Entity.Person;
using SchoolLibrary.Utils;
using System.Text.RegularExpressions;

namespace SchoolLibrary.Desktop
{
    public partial class GiveBookForm : Form
    {
        private Pupil pupil;
        private DialogResult dialogResult;

        public GiveBookForm()
        {
            InitializeComponent();
        }

        private void selectPupilButton_Click(object sender, EventArgs e)
        {
            List<Pupil> books = PupilDAO.getInstance().findAll();
            PupilGuide guide = new PupilGuide(ViewModeEnum.SELECT);
            guide.initData();
            guide.ShowDialog();
            pupil = guide.SelectedObject;
            if (pupil != null)
            {
                this.pupilTextBox.Text = pupil.AbbreviatedName;
            }
            validate();          
        }

        private void addBookButton_Click(object sender, EventArgs e)
        {
            BookGuide guide = new BookGuide(ViewModeEnum.SELECT);
            guide.initData();
            guide.ShowDialog();
            Book book = guide.SelectedObject;
            if (book != null)
            {
                this.bookList.Items.Add(book);
                validate();
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            if (validate())
            {
                foreach (object obj in bookList.Items)
                {
                    RegistryJournal record = new RegistryJournal();
                    record.Bail = bailTextBox.Text.Length > 0 ? Int32.Parse(bailTextBox.Text) : 0;
                    record.DayAmount = dayCountTextBox.Text.Length > 0 ? Int32.Parse(dayCountTextBox.Text) : 0;
                    record.Pupil = pupil;
                    record.IssueDate = DateTime.Now;
                    record.Book = (Book)obj;
                    RegistryJournalDAO.getInstance().save(record);
                    this.Close();
                }
            }
            
        }

        private void bailTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!StringUtil.isDigit(e.KeyChar))
            {
                e.Handled = true;
            }           
        }

        private void dayCountTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!StringUtil.isDigit(e.KeyChar))
            {
                e.Handled = true;
            }  
        }

        private void removeBookButton_Click(object sender, EventArgs e)
        {
            this.bookList.Items.Remove(this.bookList.SelectedItem);
            validate();
        }

        private bool validate()
        {
            bool isValid = this.bookList.Items.Count > 0 && pupil != null;
            this.okButton.Enabled = isValid;
            return isValid;
        }

        public DialogResult DialogResult
        {
            get { return dialogResult; }
        } 
    }
}
